package com.yunkaweilai.android.activity.operation.consumption.room;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.classic.common.MultipleStatusView;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.MainActivity;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.fragment.operation.room.RoomTypeListFragment;
import com.yunkaweilai.android.model.Event;
import com.yunkaweilai.android.model.operation.room.RoomTypeModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 房台消费
 * Created by Administrator on 2017/7/27.
 */

public class RoomConsumptionActivity extends BaseActivity {

    @BindView(R.id.id_tabLayout)
    TabLayout idTabLayout;
    @BindView(R.id.id_viewPager)
    ViewPager idViewPager;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;
    public static final String ROOM_TYPE = "ROOM_TYPE";
    public static final String ROOM_GID = "ROOM_GID";
    private String type;
    private String gids;
    private ArrayList<RoomTypeModel.DataBean.ListBean>list = new ArrayList<>();
    public static void actionStart(Context context,String type,String gids){
        Intent intent = new Intent(context,RoomConsumptionActivity.class);
        intent.putExtra(ROOM_TYPE,type);
        intent.putExtra(ROOM_GID,gids);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_consumption);
        ButterKnife.bind(this);
        type = getIntent().getStringExtra(ROOM_TYPE);
        gids = getIntent().getStringExtra(ROOM_GID);
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
        new TitleBuilder(mAc).setTitleText("房台消费")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if ("1".equals(type)){
                            intent2Activity(MainActivity.class);
                        }else{
                            finish();
                        }
                    }
                });
        idMultipleStatusView.showLoading();
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initHttp();
            }
        });
        initHttp();
    }

    @Override
    public void onBackPressed() {
        if ("1".equals(type)){
            intent2Activity(MainActivity.class);
        }else{
            finish();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onShopEvent(Event.TypeEvent event){
        if (event!=null){
            if (event.type == CommonConstants.SHOP_LIST_REFRESH) {
                if (event.flag){
                    initHttp();
                }
            }
        }
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.ROOM_TYPE)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode){
                            Gson gson = new Gson();
                            list.clear();
                            RoomTypeModel roomTypeModel = gson.fromJson(json, RoomTypeModel.class);
                            RoomTypeModel.DataBean.ListBean listBean = new RoomTypeModel.DataBean.ListBean();
                            listBean.setId("0");
                            listBean.setCategory_name("全部");
                            list.add(listBean);
                            list.addAll(roomTypeModel.getData().getList());
                            RoomTypeAdapter adapter = new RoomTypeAdapter(getSupportFragmentManager(),list);
                            idViewPager.setAdapter(adapter);
                            idTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
                            idTabLayout.setupWithViewPager(idViewPager);
                            if (idMultipleStatusView!=null){
                                idMultipleStatusView.showContent();
                            }
                        }else{
                            if (idMultipleStatusView!=null){
                                idMultipleStatusView.showError();
                            }
                        }

                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultipleStatusView!=null){
                            idMultipleStatusView.showError();
                        }
                    }
                });
    }

    class RoomTypeAdapter extends FragmentStatePagerAdapter {
        private List<RoomTypeModel.DataBean.ListBean> list;

        public RoomTypeAdapter(FragmentManager fm, List<RoomTypeModel.DataBean.ListBean> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return list.get(position).getCategory_name();
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = RoomTypeListFragment.getInstentce(list.get(position).getId(),type,gids);
            return fragment;
        }

        @Override
        public int getCount() {
            return list.size();
        }

    }

}
